#!/bin/bash
#current date str
date_str=`date +%Y-%m-%d-%H-%M-%S`
#temp dir
destDir='temp'
#new hosts file name
destFile="$destDir/hosts_$date_str"
#create temp dir
if [ ! -d "$destDir" ]; then
	mkdir $destDir
fi
#add update date
echo -e "#update date : $date_str\n" >> $destFile
#merge hosts
#urls="$* https://raw.githubusercontent.com/googlehosts/hosts/master/hosts-files/hosts"
urls="$*"
for url in ${urls[*]}
do
	echo "start download $url"
	echo -e "#start $url\n" >> $destFile
	curl $url >> $destFile
	echo -e "#end $url" >> $destFile
	echo "end download $url"
done
#update hosts
mv "/etc/hosts" "/etc/hosts.bak_$date_str"
cp $destFile "/etc/hosts"
#for windows
windows_hosts='/c/Windows/System32/drivers/etc/hosts'
if [ -e "$windows_hosts" ]; then
	cp $destFile $windows_hosts
fi
#remove temp dir
rm -rf temp
